﻿using FAQ_UI.window;
using java.io;
using java.nio.channels;
using System;
using System.Windows;
using yzrilyzr.crypto.mode;
using yzrilyzr.FAQ.client;
using yzrilyzr.FAQ.constants;
using yzrilyzr.FAQ.protocol;
using yzrilyzr.FAQ.server;
using yzrilyzr.FAQ.utils;
using yzrilyzr.util;

namespace FAQ_UI
{
	/// <summary>
	/// App.xaml 的交互逻辑
	/// </summary>
	public partial class App : Application, MsgPackReceiver
	{
		public static readonly int APPLICATION_CLOSE = 30001;
		public static readonly int OPEN_SETTINGS = 30002;
		public static readonly int OPEN_ACCOUNT = 30003;
		public static readonly int OPEN_CHAT = 30004;
		public static readonly int CLOSE_CHAT = 30005;
		public static readonly int OPEN_PUBKEY_SET = 30006;
		public static readonly int OPEN_STICKER_TO = 30007;
		public static readonly int CLOSE_STICKER = 30008;
		public static readonly int UPDATE_STICKER = 30009;
		public static readonly int SET_CHAT_REPLY = 30010;
		public static readonly int BIGDATA_UPLOAD = 30011;
		public static readonly int BIGDATA_DOWNLOAD = 30012;
		public static readonly int BIGDATA_MONITOR_UPDATE = 30013;
		public static readonly int BIGDATA_OPEN = 30014;
		private Logger logger = new Logger("App");

		public App()
		{
			AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
			Logger.setPrintToConsole(true);
			Logger.setPrintException(true);
			Logger.setEnabled(true);
			FAQUtil.settings = DB.from(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/FAQ/settings");
			FAQClient.setClientPath(FAQUtil.getClientPath());
			FAQServer.setServerPath(FAQUtil.getServerPath());
			FAQUtil.eventBus = new yzrilyzr.FAQ.utils.FAQEventBus();
			FAQUtil.eventBus.addReceiver(this);
		}

		private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
		{
			//处理应用程序域中的异常
			Exception ex = e.ExceptionObject as Exception;
			if (ex != null)
			{
				MessageBox.Show($"发生了未处理的异常：{ex.Message}\n{ex.StackTrace}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
			}
			else
			{
				MessageBox.Show($"发生了未处理的非托管异常", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
			}
		}

		public void receive(MsgPackTransfer mpt, MsgPack p)
		{
			int cmd = p.getCmd();
			if (cmd == OPEN_SETTINGS)
			{
				Dispatcher.Invoke(new Action(() => { new SettingsWindow().Show(); }));
			}
			else if (cmd == BIGDATA_UPLOAD)
			{
				MsgPack filePack = (MsgPack)p.get(K.DATA);
				ProgressMonitor monitor = (ProgressMonitor)ClientUtils. bigdata.get(filePack);
				if (monitor == null)
				{
					try
					{
						ClientUtils.cacheBigData(FAQUtil.client,filePack, new FileInputStream(filePack.getString(K.LOCALURI)),new ProgressListener1(filePack));
					}
					catch (FileNotFoundException e)
					{
						e.printStackTrace();
					}
				}
				else if (monitor.getState() == ProgressMonitor.State.RUNNING) monitor.pause();
				else if (monitor.getState() == ProgressMonitor.State.PAUSED) monitor.start();
			}
			else if (cmd == BIGDATA_DOWNLOAD)
			{
				MsgPack filePack = (MsgPack)p.get(K.DATA);
				ProgressMonitor monitor = (ProgressMonitor)ClientUtils.bigdata.get(filePack);
				if (monitor == null)
				{
					ClientUtils.getBigData(FAQUtil.client, filePack,FAQUtil.getDownloadPath(),new ProgressListener1(filePack));
				}
				else if (monitor.getState() == ProgressMonitor.State.RUNNING) monitor.pause();
				else if (monitor.getState() == ProgressMonitor.State.PAUSED) monitor.start();
			}
			else if (cmd == BIGDATA_OPEN)
			{
				FAQUtil.openDownloadBigData((MsgPack)p.get(K.DATA), p.getTo());
			}else if(cmd== ToolBar.TOOLBAR_SELECTOR)
			{
				FAQUtil.client.ping(5000);
			}
		}
	}
	class ProgressListener1 : ProgressMonitor.ProgressListener
	{
		MsgPack filePack;
		public ProgressListener1(MsgPack filePack){
			this.filePack = filePack;
		}
		void ProgressMonitor.ProgressListener.onProgressUpdate(ProgressMonitor.State pms, int i)
		{
			FAQUtil.eventBus.post(new BinMsgPack(App.BIGDATA_MONITOR_UPDATE).put(K.DATA, filePack)
																						.put("state", pms.name()));
		}
	}
}